package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class VentanaInicial extends JFrame{

	 private javax.swing.JButton aceptar;
	    private javax.swing.ButtonGroup buttonGroup1;
	    private javax.swing.JButton cargarmapa;
	    private javax.swing.JRadioButton civil;
	    private javax.swing.JButton jButton2;
	    private javax.swing.JLabel jLabel1;
	    private javax.swing.JLabel jLabel2;
	    private javax.swing.JRadioButton militar;
	    private String ruta,robot;
	    private boolean termine=false;
	    private VentanaJuego principal;
	    // End of variables declaration
	    
	public VentanaInicial(VentanaJuego v) {
		principal=v;
		this.setTitle("Linguo2 - Iniciar");
        this.setIconImage((new ImageIcon(getClass().getResource("Imagenes/Robot.png")).getImage()));
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        
        jLabel1.setIcon(new ImageIcon(getClass().getResource("Imagenes/CivilAbajo.gif")));
        jLabel2 = new javax.swing.JLabel();
        jLabel2.setIcon(new ImageIcon(getClass().getResource("Imagenes/MilitarArriba.gif")));
        
        civil = new javax.swing.JRadioButton();
        militar = new javax.swing.JRadioButton();
        buttonGroup1=new ButtonGroup();
        buttonGroup1.add(civil);
        buttonGroup1.add(militar);
        
        cargarmapa = new javax.swing.JButton();
        
        OyenteCargar oc= new OyenteCargar();
        cargarmapa.addActionListener(oc);
        aceptar = new javax.swing.JButton();
        OyenteAceptar oa= new OyenteAceptar();
        aceptar.addActionListener(oa);

        jButton2.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        civil.setText("Civil");
       

        militar.setText("Militar");

        cargarmapa.setText("Cargar Mapa");

        aceptar.setText("Aceptar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cargarmapa, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(civil))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(militar))))
                .addGap(47, 47, 47))
            .addGroup(layout.createSequentialGroup()
                .addGap(134, 134, 134)
                .addComponent(aceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(militar)
                    .addComponent(civil))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(cargarmapa, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(aceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }
    
    public class OyenteCargar implements ActionListener{
    	public void actionPerformed(ActionEvent arg0){
    		
    		JFileChooser elegir = new JFileChooser("Niveles");
            int opcion = elegir.showOpenDialog(cargarmapa);
       
            //Si presionamos el boton ABRIR en pathArchivo obtenemos el path del archivo
            if (opcion == JFileChooser.APPROVE_OPTION) {
                ruta= elegir.getSelectedFile().getPath(); //Obtiene path del archivo
                
            }
    	}
    		
    	
    }
    
    public class OyenteAceptar implements ActionListener{
    	public void actionPerformed(ActionEvent arg0){
    	 if (civil.isSelected())
    		 robot="Civil";
    	 	
    		 else
    			 if (militar.isSelected())
    				 robot="Militar";
    	if (robot==null||ruta==null)
    		JOptionPane.showMessageDialog(null,"Complete todos los campos","Error",JOptionPane.ERROR_MESSAGE);
    	else{
    		
    		principal.iniciar(ruta,robot);
    		dispose();
    	}
    			 
    	 }	
    }
    
    public String getRuta(){
    	return ruta;
    }
    
    public String getRobot(){
    	return robot;
    }

	public boolean termino() {
		// TODO Auto-generated method stub
		return termine;
	}
    
    
}
